// import { graphicStartEditing } from "@/utils/global.function";
import { closeContextMenu } from "@/utils/global.function";
import { ElMessage, ElMessageBox } from "element-plus";
import { useStore } from "@/stores/index1";

import storages from "@/utils/storage";
// import { useStoreData } from "@/stores/data";
import { updateFloaderName, updateFloaderLock, addFloader, deleteFloader, deleteFloaderAllData } from "@/api/coverage";
import { removeDrawData } from "@/api/draw";
const store = useStore();
// const store = useStore();
import { useStoreData } from "@/stores/data";
const storeData = useStoreData();

// import global from "@/utils/global.data.params";
import cMenu from "@/utils/contextmenu";
// import { ref } from "vue";
const contextMenu: any = [
    // 图层数据
    {
        name: "属性",
        icon: "fa fa-edit",
        separate: true,
        authority: true,
        identification: "draw",
        click: () => {
            // 激活编辑状态
            // cMenu.property();
            const { node, graphic } = getData();
            // console.log(graphic);
            // return;
            if (graphic) {
                // 打开编辑框
                store.property = true;
                graphic.flyTo();
                graphic.startEditing();
            }
            closeContextMenu();
        },
    },
    // {
    //     name: "移动(偏移)",
    //     icon: "fa fa-exchange",
    //     separate: true,
    //     authority: true,
    //     identification: "plotting,tif",
    //     click: () => {
    //         // if (editorware.treeSelectNode.data.type == 'tile') {
    //         //     // 瓦片偏移
    //         //     closeContextMenu()
    //         //     // MessageBox.alert('功能正在开发中', '', {
    //         //     //     confirmButtonText: '确定',
    //         //     //     callback: action => {
    //         //     //         editorware.rightClickData = ''
    //         //     //     }
    //         //     // })
    //         //     cMenu.tileAdjustment()
    //         // } else {
    //         //     // tif偏移
    //         //     closeContextMenu()
    //         //     cMenu.skewingAmend()
    //         // }
    //     },
    // },
    {
        name: "剪切",
        icon: "fa fa-cut",
        authority: true,
        identification: "draw,xyz,folder",
        click: () => {
            // closeContextMenu()
            // cMenu.copyShearGraphic('shear')
        },
    },
    {
        name: "复制",
        icon: "fa fa-clone",
        authority: true,
        identification: "draw,xyz,folder",
        click: () => {
            // closeContextMenu()
            // cMenu.copyShearGraphic('copy')
        },
    },
    {
        name: "删除",
        icon: "fa fa-trash-o",
        authority: true,
        identification: "draw,xyz,folder",
        click: () => {
            closeContextMenu();
            const { node, graphic } = getData();
            if (node.isLocked == "1") {
                ElMessageBox.alert("请先解锁后再进行操作!");
            } else {
                ElMessageBox.confirm(`确定要删除${node.name}吗?`, "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }).then(() => {
                    removeDrawData({
                        id: node.id,
                    }).then((res) => {
                        ElMessage.success("删除成功");
                        store.graphicLayer.removeGraphic(storages.get("coverageMap").get(node.id));
                        (storeData.tree as any).remove(node);
                        storages.get("coverageMap").delete(node.id);
                    });
                });
            }
        },
    },
    // 文件夹
    // {
    //     name: "剪切",
    //     identification: "folder,tif,kml",
    //     authority: true,
    //     icon: "fa fa-cut",
    //     click: () => {
    //         // closeContextMenu()
    //         // cMenu.copyShearFolder('shear')
    //     },
    // },
    // {
    //     name: "复制",
    //     identification: "folder,tif,kml",
    //     authority: true,
    //     icon: "fa fa-clone",
    //     click: () => {
    //         // closeContextMenu()
    //         // cMenu.copyShearFolder('copy')
    //     },
    // },
    {
        name: "粘贴",
        identification: "folder",
        authority: true,
        icon: "fa fa-files-o",
        click: () => {
            // closeContextMenu()
            // cMenu.pasteFolder()
        },
    },
    {
        name: "另存为",
        identification: "folder",
        separate: true,
        authority: true,
        icon: "fa fa-file-o",
        click: () => {
            // closeContextMenu()
            // cMenu.exportFolderJson()
        },
    },
    {
        name: "统计",
        identification: "folder",
        authority: true,
        separate: true,
        icon: "fa fa-bar-chart",
        click: () => {
            // closeContextMenu();
            cMenu.statistics();
        },
    },
    {
        name: "重命名",
        identification: "xyz,folder",
        authority: true,
        icon: "fa fa-edit",
        click: () => {
            closeContextMenu();
            const { node, graphic } = getData();
            ElMessageBox.prompt("请输入文件夹名称", "重命名", {
                inputValue: node.name,
                confirmButtonText: "确定",
                cancelButtonText: "取消",
            }).then(({ value }) => {
                if (value) {
                    updateFloaderName({
                        id: node.id,
                        folderName: value,
                    }).then((res) => {
                        node.name = value;
                        ElMessage.success("修改成功");
                    });
                } else {
                    ElMessage.error("名称不能为空");
                }
            });
        },
    },
    // {
    //     name: "批量风格",
    //     identification: "folder,kml",
    //     authority: true,
    //     icon: "el-icon-files",
    //     click: () => {
    //         // closeContextMenu()
    //         // showDialog()
    //     },
    // },
    {
        name: "删除内容",
        identification: "folder",
        authority: true,
        icon: "fa fa-trash-o",
        click: () => {
            closeContextMenu();
            // deleteFloaderAllData
            const { node, graphic } = getData();
            if (node.isLocked == "1") {
                ElMessageBox.alert("请先解锁后再进行操作!");
            } else {
                if (node.children && node.children.length == 0) {
                    ElMessage.error("该文件夹内没有内容!");
                    return false;
                }
                ElMessageBox.confirm(`确定要删除该文件夹内的所有内容吗?`, "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }).then(() => {
                    deleteFloaderAllData({
                        id: node.id,
                    }).then((res) => {
                        ElMessage.success("删除成功");
                        clearFloaderData(node);
                    });
                });
            }
        },
    },
    {
        name: "锁定",
        identification: "folder",
        authority: true,
        separate: true,
        icon: "fa fa-lock",
        click: () => {
            closeContextMenu();
            const { node, graphic } = getData();
            updateFloaderLock({
                id: node.id,
                isLocked: 1,
            }).then((res) => {
                node.isLocked = 1;
                locks(node.children, 1);
                ElMessage.success("操作成功");
            });
        },
    },
    {
        name: "添加文件夹",
        identification: "folder",
        authority: true,
        icon: "fa fa-folder-open-o",
        click: () => {
            closeContextMenu();
            ElMessageBox.prompt("请输入文件夹名称", "添加文件夹", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
            }).then(({ value }) => {
                if (value) {
                    const { node, graphic } = getData();
                    addFloader({
                        parentFolder: node.id,
                        folderName: value,
                    }).then((res) => {
                        console.log(res);
                        node.children.push(res.data);
                        ElMessage.success("添加成功");
                    });
                } else {
                    ElMessage.error("名称不能为空");
                }
            });
        },
    },
    {
        name: "删除文件夹",
        identification: "folder",
        authority: true,
        icon: "fa fa-trash-o",
        click: () => {
            closeContextMenu();
            ElMessageBox.confirm("此操作将永久删除该文件夹, 是否继续?", "删除文件夹", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                const { node, graphic } = getData();
                deleteFloader({
                    id: node.id,
                }).then((res) => {
                    (storeData.tree as any).remove(node.id);
                    ElMessage.success("删除成功");
                });
            });
        },
    },
    // {
    //     name: "上传KML文件",
    //     identification: "folder,kml",
    //     authority: true,
    //     icon: "fa fa-upload",
    //     click: () => {
    //         // closeContextMenu()
    //         // mVm.$confirm('此操作将永久删除该文件夹, 是否继续?', '删除文件夹', {
    //         //     confirmButtonText: '确定',
    //         //     cancelButtonText: '取消',
    //         //     cancelButtonClass: 'el-button--primary',
    //         //     customClass: 'inputFolder',
    //         //     type: 'warning'
    //         // }).then(() => {
    //         //     cMenu.removeFolder()
    //         // })
    //     },
    // },
    // {
    //     name: "上传TIF文件",
    //     identification: "folder,tif",
    //     authority: true,
    //     icon: "fa fa-upload",
    //     click: () => {
    //         // closeContextMenu()
    //         // mVm.$confirm('此操作将永久删除该文件夹, 是否继续?', '删除文件夹', {
    //         //     confirmButtonText: '确定',
    //         //     cancelButtonText: '取消',
    //         //     cancelButtonClass: 'el-button--primary',
    //         //     customClass: 'inputFolder',
    //         //     type: 'warning'
    //         // }).then(() => {
    //         //     cMenu.removeFolder()
    //         // })
    //     },
    // },
    // 解锁
    {
        name: "解除锁定",
        identification: "unlock",
        authority: true,
        icon: "fa fa-unlock",
        click: () => {
            closeContextMenu();
            // cMenu.unlockFolder()
            const { node, graphic } = getData();
            updateFloaderLock({
                id: node.id,
                isLocked: 0,
            }).then((res) => {
                node.isLocked = 0;
                locks(node.children, 0);
                ElMessage.success("操作成功");
            });
        },
    },
];
export default contextMenu;
/**
 * 锁定-解锁文件夹递归函数
 */
const locks = (data: any, value: Number) => {
    data.forEach((v: any, i: any) => {
        v.isLocked = value;
        if (v.children && v.children.length > 0) {
            locks(v.children, value);
        }
    });
};
/**
 * 清空文件夹标绘
 */
const clearFloaderData = (data: any) => {
    data.children.forEach((v: any) => {
        if (v.type != "folder" && v.pid == data.id) {
            setTimeout(() => {
                store.graphicLayer.removeGraphic(storages.get("coverageMap").get(v.id));
                (storeData.tree as any).remove(v);
                storages.get("coverageMap").delete(v.id);
                (storeData.tree as any).setChecked(data.id, false);
            }, 300);
        } else {
            clearFloaderData(v);
        }
    });
};
/**
 * 获取当前要操作的数据ID
 */
const getData = () => {
    const node = (storeData.tree as any).getNode(storages.get("graphicId")).data;
    // const graphic = storages.get(storages.get("graphicId"));
    const graphic = store.graphicLayer.getGraphicById(storages.get("graphicId"));
    console.log(node, graphic);
    // console.log(store.graphicLayer.getGraphicById(storages.get("graphicId")));
    return { node, graphic };
};
